﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using NmbpProject2.BusinessLayer;
using NmbpProject2.DboClasses;

namespace NmbpProject2.PresentationLayer
{
    public partial class CategoryForm : Form
    {
        public CategoryForm()
        {
            InitializeComponent();
            refreshDataSource();
        }

        private void refreshDataSource()
        {
            CategoryBL cBL = new CategoryBL();
            List<Category> allCategories = cBL.getAllCategories();
            dataGridViewCategory.DataSource = allCategories;
            dataGridViewCategory.Refresh();
        }

        /// <summary>
        /// Metoda koja se aktivira klikom na gumb za brisanje označene
        /// kategorije ili kategorija
        /// </summary>
        private void buttonDeleteCategory_Click(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection selectedRows = dataGridViewCategory.SelectedRows;
            if (selectedRows.Count > 0)
            {
                List<Category> cats = new List<Category>();
                CategoryBL cBL = new CategoryBL();
                for (int i = 0; i < selectedRows.Count; i++)
                {
                    cats.Add((Category) selectedRows[i].DataBoundItem);
                }
                if (MessageBox.Show(
                        "Are you sure you want to delete selected categories?",
                        "Confirm delete",
                        MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    try
                    {
                        cBL.delete(cats, true);
                    }
                    catch (Exception exception)
                    {
                        string message = String.Concat(
                            exception.Message,
                            "\n\nAre you sure you want to continue?"
                            );
                        if (MessageBox.Show(message, "Confirm delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            cBL.delete(cats, false);
                        }
                    }
                }
                refreshDataSource();
            }
        }

        /// <summary>
        /// Metoda koja prikazuje odabrane kategorije u text editu
        /// </summary>
        private void dataGridViewCategory_SelectionChanged(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection selectedRows = dataGridViewCategory.SelectedRows;
            if (selectedRows.Count == 1)
            {
                Category category = (Category) selectedRows[0].DataBoundItem;
                txtEditName.Text = category.Name;
            }
            else
                txtEditName.Text = "";
        }

        /// <summary>
        /// Metoda koja dodaje novu kategoriju pritiskom na gumb Add
        /// </summary>
        private void buttonAddCategory_Click(object sender, EventArgs e)
        {
            try
            {
                string Name = txtAddName.Text;

                if (Name.Length == 0)
                {
                    throw new Exception("The Name cannot be empty!");
                }
                CategoryBL cBL = new CategoryBL();
                try
                {
                    cBL.insertCategory(Name);
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.Message);
                }
                refreshDataSource();
                txtAddName.Text = "";
            }

            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }

        /// <summary>
        /// Metoda koja editira označenu kategoriju
        /// </summary>
        private void buttonEditCategory_Click(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection selectedRows = dataGridViewCategory.SelectedRows;
            if (selectedRows.Count == 1)
            {
                Category category = (Category) selectedRows[0].DataBoundItem;

                try
                {
                    string name = txtEditName.Text;
                    if (name.Length == 0)
                        throw new Exception("The Name cannot be empty!");

                    CategoryBL cBL = new CategoryBL();
                    try
                    {
                        cBL.update(category, name);
                    }
                    catch (Exception exception)
                    {
                        MessageBox.Show(exception.Message);
                    }
                    refreshDataSource();
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.Message);
                }
            }
        }

        //kraj
    }
}